package texts;


import moment.Utiles;
import org.openqa.selenium.By;

import java.io.IOException;
import java.text.SimpleDateFormat;

/**
 * 注册类
 */
public class EnrollPage extends Utiles {
    public static String url = "http://110.41.84.205:9091/sign-up.html";
    // 注册后，账号密码存放处
    public static String userName = "";
    public static String password = "";

    public EnrollPage() {
        super(url);
    }
    // 打开注册页， 并且校验是否打开成功
    public void enrollPageRight() throws IOException {
        String text = driver.findElement(By.cssSelector("#signUpForm > div > h2")).getText();
        assert text.equals("用户注册");
        driver.findElement(By.cssSelector("#submit"));
        driver.findElement(By.cssSelector("#username"));
        cutScreen(getClass().getName());
    }


    /**
     * 注册失败
     * 1. 用户名/昵称/账号/密码为空
     * 2. 两次密码不相同
     */
    public void enrollPageFail() {
        SimpleDateFormat sim = new SimpleDateFormat("HHmmssSS");
        String time = sim.format(System.currentTimeMillis());
        userName = "user" + time;
        password = time;
        /**
         * 进行用户名已存在验证
         */
        // 初始化账号密码
        // 输入用户名
        driver.findElement(By.cssSelector("#username")).sendKeys("zhangsan");
        // 输入昵称
        driver.findElement(By.cssSelector("#nickname")).sendKeys("自动化测试用户");
        // 输入密码
        driver.findElement(By.cssSelector("#password")).sendKeys(password);
        // 确认密码
        driver.findElement(By.cssSelector("#passwordRepeat")).sendKeys(password);

        // 确认协议
        driver.findElement(By.cssSelector("#policy")).click();

        // 点击注册按钮
        driver.findElement(By.cssSelector("#submit")).click();
        // 获取报错
        String alreadyExists = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")).getText();
        // 校验
        assert "警告\n用户已存在".equals(alreadyExists);

        /**
         * 进行注册
         */
        // 更新账号
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#username")).sendKeys(userName);
        // 点击注册按钮
        driver.findElement(By.cssSelector("#submit")).click();
        String text = driver.findElement(By.cssSelector("body > div > div > div > div:nth-child(1) > div > div.card.card-md > div > h2")).getText();
        assert "用户登录".equals(text);
    }
}
